<?php
namespace Api\Controller;
use Think\Controller;
class HouseController extends CommonController {
    public function index(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['type'] = 1;
        $list['new'] = M("House")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['new'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['new'][$key]['is_top'] = 0;
            }
            $list['new'][$key]['tags'] = explode(',', $value['tag']);
        }
        $map['type'] = 2;
        $list['ershou'] = M("House")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['ershou'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['ershou'][$key]['is_top'] = 0;
            }
            $list['ershou'][$key]['tags'] = explode(',', $value['tag']);
        }
        $map['type'] = 3;
        $list['zufang'] = M("House")->where($map)->order("last_time desc")->limit(5)->select();
        foreach ($list['zufang'] as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list['zufang'][$key]['is_top'] = 0;
            }
            $list['zufang'][$key]['tags'] = explode(',', $value['tag']);
        }
        $this->ajaxReturn($list);
    }

    public function lists(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        if($_GET['type'] == 4){
            $list = M("HouseMember")->where($map)->order("is_top desc,last_time desc")->select();
        }else{
            $map['type'] = $_GET['type'];
            $list = M("House")->where($map)->order("is_top desc,last_time desc")->select();
        }
        foreach ($list as $key => $value) {
            if($value['is_top'] == 1 && $value['top_end_time'] < date("Y-m-d H:i:s")){
                $list[$key]['is_top'] = 0;
            }
            $list[$key]['tags'] = explode(',', $value['tag']);
        }
        $this->ajaxReturn($list);
    }

    public function zhongjie(){
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $info = M("HouseMember")->where($map)->find();
        $info['tags'] = explode(',', $info['tag']);
        $eq['openid'] = $info['openid'];
        $eq['type'] = 2;
        $info['ershou'] =  M("House")->where($eq)->order("last_time desc")->select();
        foreach ($info['ershou'] as $key => $value) {
            $info['ershou'][$key]['tags'] = explode(',', $value['tag']);
        }
        $eq['type'] = 3;
        $info['zufang'] =  M("House")->where($eq)->order("last_time desc")->select();
        foreach ($info['zufang'] as $key => $value) {
            $info['zufang'][$key]['tags'] = explode(',', $value['tag']);
        }
        $this->ajaxReturn($info);
    }

    public function item(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['id'] = $_GET['id'];
        $info = M("House")->where($map)->find();
        $info['tags'] = explode(",", $info['tag']);
        $info['pics'] = explode(",", $info['pics']);
        $this->ajaxReturn($info);
    }

    public function zhongjieInfo(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['id'] = $_GET['id'];

        $eq['status'] = 1;
        $eq['openid'] =  M("House")->where($map)->getField('openid');
        // $eq['openid'] =  'oRVj50IUZ1wSBqMuC1amLp9TRu3Y';
        $info = M("HouseMember")->where($eq)->find();
        $this->ajaxReturn($info);
    }

    public function zhongjieAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_POST['openid'] == 'undefined'){
            $this->error('网络忙，请稍后再试！');
        }
        $eq['openid'] = $_POST['openid'];
        if(M("HouseMember")->where($eq)->find()){
            $this->error('你已经提交过入驻申请了，请等待系统审核！');
        }

        $_POST['wid'] = session('wid');
        $_POST['create_time'] = date("Y-m-d H:i:s");
        $_POST['status'] = 0;
        $_POST['tag'] = "销售达人";

        if($info_id = M("HouseMember")->add($_POST)){
            // $this->getInfoOrder($info_id);
            
            // $map['id'] = $info_id;
            // $data['status'] = 1;
            // M("Zhongjie")->where($map)->save($data);
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function add(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        $_POST['type'] = $_GET['type'];
        $_POST['wid'] = session('wid');
        $_POST['create_time'] = date("Y-m-d H:i:s");

        if($info_id = M("House")->where($map)->add($_POST)){
            $this->getInfoOrder($info_id);
            
            $map['id'] = $info_id;
            $data['status'] = 1;
            M("House")->where($map)->save($data);
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

    }
}